package com.ydsungan.web;
import com.ydsungan.service.SigninService;
import com.ydsungan.serviceimpl.SigninServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DeleteServlet extends HttpServlet {

    private SigninService signinService = null;
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public void init() throws ServletException {
        try {
            signinService = new SigninServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        Integer id = Integer.parseInt(request.getParameter("id"));
        logger.info("请求删除的ID：" + id);

        PrintWriter writer = response.getWriter();
        try{
            signinService.deleteById(id);
            writer.write("{\"code\":\"1\",\"msg\":\"ok\"}");
            logger.info("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.info("id = " + id + "删除失败");
            writer.write("{\"code\":\"0\",\"msg\":\"failure\"}");
        }



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
